home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 8614 / 8614.xpi / modules / utils / WindowUtils.jsm < prev   
Text File  |  2010-02-10  |  4KB  |  120 lines

  1. // DO NOT import this into the global namespace, but instead
  2. // import it into your own namespace wrapper
  3.  
  4. var EXPORTED_SYMBOLS = ["WindowUtils"];
  5.  
  6. Components.utils.import("resource://glydo/utils/prototype_xul_1_6_0_3_modified.jsm");
  7. Components.utils.import("resource://glydo/utils/Utils.jsm");
  8. Components.utils.import("resource://glydo/utils/Prefs.jsm");
  9.  
  10. var WindowUtils = {
  11.     openDialog: function(parentWindow, url, windowName, features)
  12.     {
  13.         var array = Components.classes["@mozilla.org/array;1"]
  14.                               .createInstance(Components.interfaces.nsIMutableArray);
  15.         for (var i=4; i<arguments.length; i++)
  16.         {
  17.             var variant = Components.classes["@mozilla.org/variant;1"]
  18.                                     .createInstance(Components.interfaces.nsIWritableVariant);
  19.             variant.setFromVariant(arguments[i]);
  20.             array.appendElement(variant, false);
  21.         }
  22.  
  23.         var watcher = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  24.                                 .getService(Components.interfaces.nsIWindowWatcher);
  25.         return watcher.openWindow(parentWindow, url, windowName, features, array);
  26.     },
  27.  
  28.     newBrowserWindow: function(parent,urls) {
  29.         WindowUtils.openDialog(parent, "chrome://browser/content/browser.xul",
  30.                 "_blank", "chrome,dialog=no,all", urls);
  31.     },
  32.  
  33.     getCurrentBrowserWindow: function(hint) {
  34.         if (hint && hint.getBrowser && hint.getBrowser()) {
  35.             return hint;
  36.         }
  37.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  38.             .getService(Components.interfaces.nsIWindowMediator);
  39.  
  40.         var win = wm.getMostRecentWindow("navigator:browser");
  41.         if (win && win.toolbar.visible) {
  42.             return win;
  43.         }
  44.  
  45.         // else
  46.         var enumerator = wm.getEnumerator("navigator:browser");
  47.         while (enumerator.hasMoreElements()) {
  48.             win = enumerator.getNext();
  49.             if (win.toolbar.visible) {
  50.                 return win;
  51.             }
  52.         }
  53.  
  54.         return null;
  55.     },
  56.  
  57.     goToURL: function(parent, url, openMode) {
  58.     
  59.         openMode = openMode ? openMode
  60.             : Prefs.recommendations_open_mode;
  61.     
  62.         // If URL is a mailto url, open it
  63.         if (Prototype.S.startsWith(url,"mailto:")) {
  64.             var win = WindowUtils.getCurrentBrowserWindow(parent);
  65.             if (win) {
  66.                 win.getBrowser().selectedBrowser.loadURI(url);
  67.             } else {
  68.                 WindowUtils.newBrowserWindow(parent,url);
  69.             }
  70.             return "MAIL";
  71.         }
  72.         
  73.         var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  74.             .getService(Components.interfaces.nsIWindowMediator);
  75.         var browserEnumerator = wm.getEnumerator("navigator:browser");
  76.     
  77.         // Check each browser instance for our URL
  78.         var found = false;
  79.         while (!found && browserEnumerator.hasMoreElements()) {
  80.             var browserInstance = browserEnumerator.getNext().getBrowser();
  81.     
  82.             // Check each tab of this browser instance
  83.             var numTabs = browserInstance.tabContainer.childNodes.length;
  84.             for ( var index = 0; index < numTabs; index++) {
  85.                 var currentBrowser = browserInstance.getBrowserAtIndex(index);
  86.                 if (url == currentBrowser.currentURI.spec) {
  87.     
  88.                     // The URL is already opened. Select this tab.
  89.                     browserInstance.selectedTab = browserInstance.tabContainer.childNodes[index];
  90.     
  91.                     // Focus *this* browser
  92.                     browserInstance.focus();
  93.                     return "FOCUS_EXISTING";
  94.                 }
  95.             }
  96.         }
  97.     
  98.         // Our URL isn't open. Open it now.
  99.         switch (openMode) {
  100.             case 'current':
  101.                 var win = WindowUtils.getCurrentBrowserWindow(parent);
  102.                 if (win) {
  103.                     win.getBrowser().selectedBrowser.loadURI(url);
  104.                     return "CURRENT_TAB";
  105.                 }
  106.                 break;
  107.             case 'new-tab':
  108.                 var win = WindowUtils.getCurrentBrowserWindow(parent);
  109.                 if (win) {
  110.                     var newTab = win.getBrowser().addTab(url);
  111.                     win.getBrowser().selectedTab = newTab;
  112.                     return "NEW_TAB";
  113.                 }
  114.                 break;
  115.         }
  116.         WindowUtils.newBrowserWindow(parent,url);
  117.         return "NEW_WINDOW";
  118.     },
  119. }
  120.